package com.ctakit.proxy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.misc.ProxyGenerator;

public class ProxyClassFile {

	public static void main(String[] args) {

		String proxyName = "DynamicSubject";

		RealSubject rs = new RealSubject(); // 在这里指定被代理类

		Class[] interfaces = rs.getClass().getInterfaces();

		byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces);

		File f = new File("/Users/lizhenmin/tmp/test/DynamicSubject.class");

		try {
			if(!f.getParentFile().exists()) {
				f.getParentFile().mkdirs();
			}

			FileOutputStream fos = new FileOutputStream(f);

			fos.write(proxyClassFile);

			fos.flush();

			fos.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace(); // To change body of catch statement use
									// File | Settings | File Templates.

		} catch (IOException e) {

			e.printStackTrace(); // To change body of catch statement use
									// File | Settings | File Templates.

		}

	}

}